
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local Chapter2level_Cfg=_G.CommonOHOH.config.Chapter.chapter2level
local Tool=require(script.Parent.Tool)
local ItemTool=_G.CommonOHOH.load.ItemTool();


function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self, parent)

	M.super.ctor(self, parent);

	self.DataStore=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
    self.itemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl())

    self.OldRebirth=self.DataStore:GetData("base").rebirth

    self.parent.LevelPassEvent:Connect(function(levelId)
        local levels= self.DataStore:GetData("levels")
        if not levels[levelId] then
            levels[levelId]=1
        else
            levels[levelId]=levels[levelId]+1
        end
        self.parent:SynchDataToClient("levels")
    end)

end;


--解锁章节
function M.UnLockChapter(self, cid)
    
    if self.DataStore:GetData("chapter")[cid]==2 then
        return true
    end


    if not Tool.IsCanUnLock(cid,self.DataStore) then
        return false
    end

    local cfg=Chapter2level_Cfg[cid]

    if not ItemTool.IsHadEnoughByTable(cfg.unlockCost,self.DataStore) then
        return false
    end

    for k,v in pairs(cfg.unlockCost) do
        self.itemCtrl:AddAnyThing(k,-v,true,false)
    end
    self.DataStore:GetData("chapter")[cid]=2
    self.parent:SynchDataToClient("chapter","base")

    local id=string.format("world_%s",cid)
    self.parent.OnboardingFunnelEvent:Action(id) --统计：进入下一个世界



    return true
end


local function_list = _G.ServerOHOH.function_list
M:RegistFunction(function_list.unLock_chapter,M.UnLockChapter)


return M